# ------------------------------------------------------------------------------------------------
### R master-file to load all packages and run all the scripts
# ------------------------------------------------------------------------------------------------
# Set working directory
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

# Set seed for reproducibility
set.seed(1234)

# Log session information for reproducibility
session_info <- sessionInfo()

# Create detailed session information
session_details <- list(
  R_version = R.version.string,
  platform = R.version$platform,
  os = Sys.info()["sysname"],
  os_version = Sys.info()["release"],
  architecture = R.version$arch,
  timezone = Sys.timezone(),
  packages_loaded = (.packages())
)

# ------------------------------------------------------------------------------------------------
### Load required packages:
# ------------------------------------------------------------------------------------------------
# Define packages needed for the analysis
p_needed <- c("devtools", "readxl", "did", "giscoR", "ggthemes", "purrr", "ggplot2", "ggpubr",
              "lubridate", "modelsummary", "plm", "randomizr", "scales", "dplyr",
              "sf", "synthdid", "tidyverse", "MCPanel", "progress")

# Load packages and install if missing
package_results <- lapply(p_needed, require, character.only = TRUE)
names(package_results) <- p_needed

# Check if MCPanel failed to load and install from GitHub if needed
if(!package_results[["MCPanel"]]) {
  if (!require("devtools", character.only = TRUE)) {
    install.packages("devtools", repos = "https://cran.r-project.org")
    library("devtools", character.only = TRUE)
  }
  tryCatch({
    devtools::install_github("susanathey/MCPanel")
    library("MCPanel", character.only = TRUE)
    cat("Successfully installed MCPanel from GitHub\n")
  }, error = function(e) {
    warning("MCPanel could not be installed. Follow instructions to download the package here: https://github.com/susanathey/MCPanel.")
  })
}

rm(p_needed, package_results)

# ------------------------------------------------------------------------------------------------
### Load custom functions:
# ------------------------------------------------------------------------------------------------
# Source all custom functions used in the analysis
source("functions/fun_cutoff.R", local = TRUE)
source("functions/fun_sdid_custom_plot.R", local = TRUE)
source("functions/fun_estimator.R", local = TRUE)
source("functions/fun_table.R", local = TRUE)

# ------------------------------------------------------------------------------------------------
### Data preparation:
# ------------------------------------------------------------------------------------------------
# Clean and prepare election data
source("01_cleaning.R", local = TRUE)

# Merge election data with LGBTQ-free zones data
source("02_merging.R", local = TRUE)

# ------------------------------------------------------------------------------------------------
### Main analysis:
# ------------------------------------------------------------------------------------------------
# Run synthetic difference-in-differences analyses and simulations
# Note: Results are pre-saved, simulations take a long time to run
#source("03_analyses.R", local = TRUE)

# ------------------------------------------------------------------------------------------------
### Generate figures:
# ------------------------------------------------------------------------------------------------
# Create all figures for the paper
source("plots/01_figure1.R", local = TRUE)
source("plots/02_figure2.R", local = TRUE)
source("plots/03_figure5.R", local = TRUE)
source("plots/04_figure6.R", local = TRUE)
source("plots/05_figure7.R", local = TRUE)
source("plots/06_figure8.R", local = TRUE)
source("plots/07_figure9.R", local = TRUE)
source("plots/08_figure10.R", local = TRUE)
source("plots/09_figure11.R", local = TRUE)
source("plots/10_figure12.R", local = TRUE)
source("plots/11_figure13.R", local = TRUE)
source("plots/12_figure14.R", local = TRUE)
source("plots/13_figure15.R", local = TRUE)
source("plots/14_figure16.R", local = TRUE)
source("plots/15_figure17.R", local = TRUE)
source("plots/16_figure18.R", local = TRUE)
source("plots/17_figure19.R", local = TRUE)

# ------------------------------------------------------------------------------------------------
### Generate tables:
# ------------------------------------------------------------------------------------------------
# Create all tables for the paper
source("tables/01_table2.R", local = TRUE)
source("tables/02_table3.R", local = TRUE)
source("tables/03_table4.R", local = TRUE)
source("tables/04_table5.R", local = TRUE)
source("tables/05_table6.R", local = TRUE)
source("tables/06_table7.R", local = TRUE)
source("tables/07_table8.R", local = TRUE)
source("tables/08_table9.R", local = TRUE)
source("tables/09_table10.R", local = TRUE)
source("tables/10_table11.R", local = TRUE)
source("tables/11_table12.R", local = TRUE)

# ------------------------------------------------------------------------------------------------
### Log session information:
# ------------------------------------------------------------------------------------------------
save(session_info, file = "../output/logs/session_info.Rda")
save(session_details, file = "../output/logs/session_details.Rda")

